#include "PreComplie.h"
#include "BTree.h"
#include "D3D.h"

//#pragma comment(linker, "/entry:mainCRTStartup")

struct Vertex
{
	Vertex(){}
	Vertex(float x, float y, float z):_x(x),_y(y),_z(z)
	{
	}
	float _x, _y, _z;
	static const DWORD FVF = D3DFVF_XYZ;
};

LRESULT CALLBACK WndProc(HWND wnd, unsigned int msg, WPARAM wp, LPARAM lp)
{
	switch(msg)
	{
	case WM_CLOSE:
		PostQuitMessage(0);
		return 0;
	case WM_KEYDOWN:
		if(wp == VK_ESCAPE)
		{
			PostQuitMessage(0);
			return 0;
		}
	default:
		return DefWindowProc(wnd, msg, wp, lp);
	}
};

//int main(/*int argc, char* argv[]*/)
//{
//	WNDCLASS wndClass;
//	wndClass.cbClsExtra = 0;
//	wndClass.cbWndExtra = 0;
//	wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
//	wndClass.hCursor = LoadCursor(0, IDC_ARROW);
//	wndClass.hIcon = LoadIcon(0, IDI_APPLICATION);
//	wndClass.hInstance = (HINSTANCE)GetModuleHandle(NULL);
//	wndClass.lpfnWndProc = WndProc;
//	wndClass.lpszClassName = L"Test";
//	wndClass.lpszMenuName = 0;
//	wndClass.style = CS_HREDRAW | CS_VREDRAW;
//
//	if(!RegisterClassW(&wndClass))
//		return 0;
//
//	HWND wnd = CreateWindowW(L"Test", L"Test3D", WS_OVERLAPPEDWINDOW, 200, 100, 800, 600, NULL, 0, (HINSTANCE)(GetModuleHandle(NULL)), 0);
//
//	D3D::Initialize(wnd, 800, 600, false);
//	ShowWindow(wnd, SW_SHOW);
//	UpdateWindow(wnd);
//
//	IDirect3DVertexBuffer9* pVertexBuffer;
//	D3D::g_pDevice->CreateVertexBuffer(3 * sizeof(Vertex), D3DUSAGE_WRITEONLY, Vertex::FVF, D3DPOOL_MANAGED, &pVertexBuffer, 0);
//	Vertex* pVertex;
//	pVertexBuffer->Lock(0, 0, (void**)&pVertex, 0);
//	pVertex[0] = Vertex(-1, 0, 2);
//	pVertex[1] = Vertex(0, 1, 2);
//	pVertex[2] = Vertex(1, 0, 2);
//	pVertexBuffer->Unlock();
//	D3DXMATRIX matrix;
//	D3DXMatrixPerspectiveFovLH(&matrix, D3DX_PI * 0.5, 800/(float)600, 1, 10000);
//	D3D::g_pDevice->SetTransform(D3DTS_PROJECTION, &matrix);
//	D3D::g_pDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
//	
//	MSG msg;
//	while(true)
//	{
//		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
//		{
//			if(msg.message == WM_QUIT)
//				break;
//			TranslateMessage(&msg);
//			DispatchMessage(&msg);
//		}
//		else
//		{
//			D3D::g_pDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xFFFFFF00, 1, 0);
//			D3D::g_pDevice->BeginScene();
//			D3D::g_pDevice->SetStreamSource(0, pVertexBuffer, 0, sizeof(Vertex));
//			D3D::g_pDevice->SetFVF(Vertex::FVF);
//			D3D::g_pDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
//			D3D::g_pDevice->EndScene();
//			D3D::g_pDevice->Present(0, 0, 0, 0);
//		}
//		Sleep(100);
//	}
//	return 0;
//}
